import api from "../../api/index";
import uploadFileOss from "../../utils/uploadFileOss/index";

const app = getApp()

Component({
    properties: {
        showModal: {
            type: Boolean,
            default: false
        },

        userInfoNew: {
            type: Object,
            value: {}
        }
    },

    data: {
        showModalNickname: false
    },

    methods: {
        onChooseAvatar(e) {
            console.log(e);
            var num = []
            num.push(e.detail.avatarUrl)
            console.log(num);
            console.log(num);
            this.uploadDIY(
                num,
                0,
                0,
                0,
                num.length,
                num
            )
        },

        onClose() {
            this.setData({
                showModal: false
            })
        },

        uploadDIY(filePaths, successUp, failUp, i, length, param) {
            // 正在上传
            // debugger
            param.doing = true // this.data.prove.doing = true;
            param.imagesUrl = []
            let that = this
            // wx.showLoading({
            //     title: `上传${i}/${length}张图片中`,
            //     duration: 1000
            // })
    
            uploadFileOss(filePaths[i])
                .then((res) => {
                    successUp++
                    i++
                    param.imagesUrl.push(res.url)
                    // that.setData({
                    //     prove: that.data.prove
                    // })
                    if (i === length) {
                        // 全部上传成功
                        console.log('图片上传成功后的url', param.imagesUrl, length)
                        if (successUp === length) {
                            param.uploadSuccess = true
                            console.log('图片上传成功进入回调')
                            // wx.hideLoading()
                            that.savetmp(param.imagesUrl[0])
                        } else {
                            // 图片上传部分失败
                            param.doing = false
                            wx.hideLoading()
                            wx.showToast({
                                title: '上传图片失败,请重新上传！',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    } else {
                        // 递归调用uploadDIY函数
                        that.uploadDIY(
                            filePaths,
                            successUp,
                            failUp,
                            i,
                            length,
                            param
                        )
                    }
                })
                .catch((e) => {
                    console.log('---图片上传失败 catch--- e', e)
                    param.doing = false
                    failUp++
                    wx.hideLoading()
                    wx.showToast({
                        title: '图片上传失败，请重新上传！',
                        icon: 'none',
                        duration: 2000
                    })
                })
        },

        savetmp(e) {
            this.saveUserInfo({
                wechatimg: e
            }, false)
            if (this.data.userInfoNew.nickName && this.data.userInfoNew.nickName !== '微信用户' && this.data.userInfoNew.nickName !== '') {
                console.log(this.data.userInfoNew);
                this.setData({
                    'userInfoNew.avatarUrl': e,
                    'showModal': false,
                    'isUser': true
                })
            } else {
                this.setData({
                    'userInfoNew.avatarUrl': e,
                    'userInfoNew.nickName': '',
                    'showModal': false,
                    'isUser': true,
                    'showModalNickname': true
                })
                console.log(this.data.userInfoNew);
            }
            app.getHttpUser()
        },

        saveUserInfo(data = {}) {
            return new Promise((resolve, reject) => {
                api.post('/api/user/savenickname', {
                    data: data,
                }).then(res => {
                    app.isLogin({ ctx: this }).then(() => {
                        resolve(res)
                    })
                }).catch(e => {
                    reject(e)
                })
            })
        },

        onNicknameSaved(e) {
            console.log('nickname changed', e.detail)
            this.setData({
                'userInfoNew.nickName': e.detail,
            }, () => {
                this.triggerEvent('on-info-changed', {
                    nickName: e.detail,
                    avatarUrl: this.data.userInfoNew.avatarUrl
                })
            })
        }
    },
})